<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- QC活动成果报告 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name title="报送QC活动成果报告"></title-name> -->
        <!-- 表格 -->
        <list-table
          ref="table"
          tableLayout="fixed"
          size="default"
          :columns="columns"
          :request="loadData"
          :isShowCountBar="false"
          :selectConfig="{ use: true, type: 'checkbox', rowKey: 'id' }"
          :exportPermission="{ select: [
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_aexp_08'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_aexp_08'
                      }
              ], all: [
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_sexp_07'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_sexp_07'
                      }
              ] }"
          :exportConfig="{ use: true, select: 'QC活动成果报告', all: 'QC活动成果报告' }"
        >
          <a-button slot="prefix" v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_add_01'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_add_01'
                      }
              ]" icon="plus" type="primary" @click="handleManage(2)"
            >新增活动成果报告</a-button
          >
          <a-button
            slot="suffix"
            slot-scope="scoped"
            v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_enc_09'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_enc_09'
                      }
              ]"
            :loading="downLoading"
            class="mb12"
            icon="cloud-download"
            :disabled="scoped.selectedRowKeys && scoped.selectedRowKeys.length === 0"
            type="primary"
            @click="downZip(scoped.selectedRowsMap, 'files')"
          >
            批量下载附件
          </a-button>
          <!-- 统计汇总 -->
          <AuditSummary
            slot="bottom"
            title="课题注册"
            apiName="statistics"
            :form="{ query: queryParam }"
            :loadCount="loadCount"
            :api="api"
            :countList="countList"
          />
          <template slot="actions" slot-scope="text, record">
            <MoreButton>
            <a v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_loo_04'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_loo_04'
                      }
              ]" @click="handleManage(0, record)">查看</a>
            <!-- 公司人员可编辑所有的，分公司人员只能编辑自己分公司的 -->
            <template v-if="record.status === 'DRAFT' || record.status === 'REJECTED' && record.isOperation">
              <a v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_edi_02'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_edi_02'
                      }
              ]" @click="handleManage(1, record)">编辑</a>
              <a v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_del_03'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_del_03'
                      }
              ]" @click="handleRemove(record.id)">删除</a>
            </template>
            <!-- 调整 -->
            <template v-if="record.status === 'PAST'">
              <a v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_rej_06'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_rej_06'
                      }
              ]" @click="handleManage(3, record)">变更</a>
            </template>
            <!-- 当状态为审批中 -->
            <template v-if="record.status != 'DRAFT'">
              <a v-btnPermission="[
                      {
                        ident: 'prma_seex',
                        code: 'seex_qm_042_exa_05'
                      },
                      {
                        ident: 'prma_buco',
                        code: 'buco_qm_042_exa_05'
                      }
              ]" @click="$bus.$emit('showApprovalDetailsDialog', record.id)"
                >审批详情</a
              >
            </template>
            </MoreButton>
          </template>
          <a slot="changeVersion" slot-scope="text, record" @click="handleOpen(record)">{{
            record.changeVersion ? 'V' + record.changeVersion : ''
          }}</a>
        </list-table>
      </a-card>
    </div>
    <dialog-edit
      :columns="dialogColumns"
      :visible.sync="visible"
      :api="api"
      :args="form"
      pathName="QCActivityReportEdit"
    ></dialog-edit>
  </base-breadcrumb>
</template>

<script>
import { qcActivityAchievement } from '@/api/quality/quality' // api
import Selector from '@/components/Select/Selector' // 选择器
import BasePage from '@/views/basePage'
import AuditSummary from '@/views/quality/components/AuditSummary' // 统计汇总
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import OrgTreeSelect from '@/components/OrgTreeSelect'
import dialogEdit from '@/views/quality/components/VisionHistory' // 版本详情
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态

const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   type: SelectSubCompany,
  //   valueKey: 'branchComId'
  // },
  // {
  //   name: '所属项目',
  //   type: SelectProject,
  //   keyValue: 'branchComId',
  //   valueKey: 'projectId'
  // },
  {
    name: '项目状态',
    type: SelectProjectStatus,
    valueKey: 'projectStatus'
  },
  {
    name: '活动开展年度',
    type: 'yearRange',
    value: 'year',
    attr: {
      rangeFormat: ['activeStartYear', 'activeEndYear'],
      placeholder: ['开始时间', '截止时间']
    }
  },
  {
    name: '课题类型',
    type: Selector,
    selectType: '47',
    valueKey: 'topicTypeId'
  },
  {
    name: '状态',
    type: Selector,
    selectType: '22',
    valueName: 'code',
    valueKey: 'statusCode'
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchComName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '课题名称',
    dataIndex: 'register.topicName',
    scopedSlots:{customRender:'topicName'}
  },
  {
    title: '课题注册号',
    align: 'center',
    width: 200,
    dataIndex: 'register.topicRegisterNumber',
    scopedSlots:{customRender:'topicRegisterNumber'}
  },
  {
    title: '课题类型',
    dataIndex: 'register.topicTypeName',
    scopedSlots:{customRender:'topicTypeName'}
  },
  {
    title: '小组名称',
    ellipsis: true,
    dataIndex: 'register.topicGroupName',
    scopedSlots:{customRender:'topicGroupName'}
  },
  {
    title: '小组注册号',
    align: 'center',
    ellipsis: true,
    width: 200,
    dataIndex: 'register.groupRegisterNumber',
    scopedSlots:{customRender:'groupRegisterNumber'}
  },
  {
    title: '活动开展年度',
    dataIndex: 'activeYear'
  },
  {
    title: '实际开始时间',
    dataIndex: 'actualStartTime'
  },
  {
    title: '实际结束时间',
    dataIndex: 'actualEndTime'
  },
  {
    title: '课题实际完成人',
    ellipsis: true,
    dataIndex: 'completeEmployeeName'
  },
  {
    title: '版本信息',
    dataIndex: 'changeVersionName',
    // fixed: 'right',
    scopedSlots: { customRender: 'changeVersion' }
  },
  {
    title: '状态',
    dataIndex: 'statusName',
    align: 'center',
    // fixed: 'right'
    // scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '230px',
    fixed: 'right',
    // align: 'center',
    scopedSlots: { customRender: 'actions' }
  }
]
const dialogColumns = [
  {
    title: '版本号',
    dataIndex: 'changeVersion',
    customRender(e) {
      return e ? 'V' + e : ''
    }
  },
  {
    title: '上传时间',
    dataIndex: 'createTime'
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '100px',
    scopedSlots: { customRender: 'actions' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    AuditSummary,
    dialogEdit
  },
  data() {
    this.columns = columns
    this.dialogColumns = dialogColumns
    this.searchArr = searchArr
    return {
      visible: false, // 弹窗显隐控件
      queryParam: {}, // 查询参数
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.loadCount++
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        return qcActivityAchievement.getListByQuery(requestParameters).then((res) => {
          res.data?.forEach((i) => {
            i.statusName = { PAST: '审批通过', REJECTED: '审批不通过', DRAFT: '待提交审批', SUBMITTED: '审批中' }[
              i.status
            ]
            i.changeVersionName = i.changeVersion ? 'V' + i.changeVersion : ''
          })
          return res
        })
      },
      editModel: 0,
      api: qcActivityAchievement, // 汇总api
      loadCount: 0, // 汇总加载控件
      countList: [
        {
          title: '总数：',
          value: 'total',
          needUnit: true
        },
        {
          title: '待提交审核：',
          value: 'draft',
          needUnit: true
        },
        {
          title: '审核中：',
          value: 'submit',
          needUnit: true
        },
        {
          title: '审核不通过：',
          value: 'rejected',
          needUnit: true
        },
        {
          title: '审核通过：',
          value: 'past',
          needUnit: true
        }
      ], // 汇总列表
      form: {}, // 弹窗参数
      exportLoading: {
        0: false,
        1: false
      } // 导出loading
    }
  },
  methods: {
    /**
     * 查看历史版本
     */
    handleOpen(e) {
      this.form.id = e.id
      this.visible = true
    },
    /**
     * 删除草稿:api 请求api;params 请求参数为对象
     */
    handleRemove(id) {
      this.$confirm({
        title: '温馨提示',
        content: '请确定是否要删除该条数据？',
        onOk: () => {
          return this.api.handleDelete(id).then((res) => {
            if (res.data.code === 200) {
              this.$message.success('删除成功！')
              this.$refs.table.loadData()
            }
          })
        }
      })
    },
    // 搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'QCActivityReportEdit',
        query
      })
    }
  }
}
</script>
